package com.neusoft.nep.common;

import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

public class ViewSwitcher {
    public static void switchView(Button  btn ,String fxmlPath) {
        if (btn == null || btn.getText() == null) {
            Dialog.showErrorDialog("", "按钮尚未附加到场景图中，无法切换视图！");
            return;
        }
        try {
            FXMLLoader fxmlLoader = new FXMLLoader();
            URL url = ViewSwitcher.class.getResource(fxmlPath);
            if (url == null) {
                throw new IllegalArgumentException("Invalid FXML path:" + fxmlPath);
            }
            fxmlLoader.setLocation(url);
            Parent root = fxmlLoader.load();
            Scene scene = btn.getScene();
            scene.setRoot(root);
            Stage stage = (Stage) scene.getWindow();
            stage.setScene(scene);
            stage.show();
        } catch (IOException e) {
            e.printStackTrace();
            Dialog.showErrorDialog("ERROR", "switchView打开失败");
        }

    }
}